package com.ias.agdis_r.action.task;

import android.content.Context;
import android.text.TextUtils;

import com.ias.agdis_r.R;
import com.ias.agdis_r.bean.auth.AuthEnum;
import com.ias.agdis_r.bean.task.FLG10TaskBean;
import com.ias.agdis_r.bean.task.FLG10TaskInfoBean;
import com.ias.agdis_r.bean.task.LuggageBarCode;
import com.ias.agdis_r.bean.task.Task;
import com.ias.agdis_r.bean.task.TaskForward;
import com.ias.agdis_r.bean.task.TaskInfo;
import com.ias.agdis_r.bean.task.TaskMonitorInfo;
import com.ias.agdis_r.bean.task.TaskServiceLabel;
import com.ias.agdis_r.bean.user.User;
import com.ias.agdis_r.bean.user.UserInfoBean;
import com.ias.agdis_r.db.BoxDBHelper;
import com.ias.agdis_r.impl.task.TaskResponseImpl;
import com.ias.agdis_r.manager.ActionManager;
import com.ias.agdis_r.manager.DataBaseManager;
import com.ias.agdis_r.manager.DataManager;
import com.ias.agdis_r.manager.EffectManager;
import com.ias.agdis_r.manager.FindLuggageDataManager;
import com.ias.agdis_r.manager.JavaBeanManager;
import com.ias.agdis_r.manager.ThreadManager;
import com.ias.agdis_r.module.MessageModel;
import com.ias.agdis_r.module.ParameterModel;
import com.ias.agdis_r.terminalmanager.restful.base.endpoint.response.BaseResponse;
import com.ias.agdis_r.terminalmanager.restful.base.endpoint.response.BaseResponseHeader;
import com.ias.agdis_r.terminalmanager.restful.binding.endpoint.response.ManCarBindingResponse;
import com.ias.agdis_r.terminalmanager.restful.findluggage.endpoint.response.FindLuggageResponse;
import com.ias.agdis_r.terminalmanager.restful.findluggage.endpoint.response.ReportFindLuggageResponse;
import com.ias.agdis_r.terminalmanager.restful.findluggage.vo.FindLuggageInfoResponseVO;
import com.ias.agdis_r.terminalmanager.restful.findluggage.vo.LuggageMessageVO;
import com.ias.agdis_r.terminalmanager.restful.findluggage.vo.LuggageVO;
import com.ias.agdis_r.terminalmanager.restful.findluggage.vo.ReportFindLuggageResponseVO;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.response.DeleteLuggageResponse;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.response.FindForwardTaskResponse;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.response.ForwardTaskResponse;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.response.GetGroupmemberResponse;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.response.GetHandOverUserResponse;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.response.GetUnfinishedEventTaskListsResponse;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.response.GetnewplantasklistsResponse;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.response.GettaskbytnbResponse;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.response.GetunfinishedtasklistsResponse;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.response.QueryLuggageResponse;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.response.SubmittaskmarkResponse;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.response.SubmittaskprocssResponse;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.response.SubmittaskvnbResponse;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.response.TaskTransferredResponse;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.response.TransferTaskResponse;
import com.ias.agdis_r.terminalmanager.restful.task.endpoint.response.UploadLuggageResponse;
import com.ias.agdis_r.terminalmanager.restful.task.vo.FindForwardTaskResponseVO;
import com.ias.agdis_r.terminalmanager.restful.task.vo.GetTaskByTnbResponseVO;
import com.ias.agdis_r.terminalmanager.restful.task.vo.SubmitTaskMarkResponseVO;
import com.ias.agdis_r.terminalmanager.restful.task.vo.SubmitTaskVnbResponseVO;
import com.ias.agdis_r.terminalmanager.restful.task.vo.TaskTransferredResponseVO;
import com.ias.agdis_r.terminalmanager.restful.task.vo.UploadLuggageResultVO;
import com.ias.agdis_r.terminalmanager.restful.workgroup.response.WorkGroupAddMemberResponse;
import com.ias.agdis_r.terminalmanager.restful.workgroup.response.WorkGroupDeleteMembersResponse;
import com.ias.agdis_r.terminalmanager.restful.workgroup.response.WorkGroupLoadMembersResponse;
import com.ias.agdis_r.terminalmanager.restful.workgroup.vo.Member;
import com.ias.agdis_r.terminalmanager.restful.workgroup.vo.WorkGroupAddMemberResponseVO;
import com.ias.agdis_r.terminalmanager.restful.workgroup.vo.WorkGroupLoadMembersResponseVO;
import com.ias.agdis_r.util.S;
import com.ias.agdis_r.util.ServiceLabelUtil;
import com.ias.agdis_r.util.ShowToast;
import com.ias.agdis_r.util.TaskUtil;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class TaskResponseAction implements TaskResponseImpl {
	private Context context = DataBaseManager.getInstance().getContext();

	@Override
	public boolean receiveITSD(BaseResponse response) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean receiveTTSD(BaseResponse response) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean receiveITDD(BaseResponse response) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean receiveTMRS(BaseResponse response) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean receiveTMRP(BaseResponse response) {
		// TODO Auto-generated method stub
		SubmittaskmarkResponse submittaskmarkResponse = (SubmittaskmarkResponse) response;
		SubmitTaskMarkResponseVO submittaskmarkResponseVO = submittaskmarkResponse
				.getSubmitTaskMarkResponseVO();

		String sts = submittaskmarkResponseVO.getStat();
		// 如果上报成功
		if ("S".equals(sts)) {
			// 根据tnb和mki来取得发送服务标注信息的内容
			String tnb = submittaskmarkResponseVO.getTnb();
			String mki = submittaskmarkResponseVO.getMkid();
			String reportServiceContent = ServiceLabelUtil
					.getReportServiceLabel(tnb + mki);
			if (reportServiceContent == null) {
				return false;
			}
			Task task = DataManager.getInstance().getWorkTask(tnb);
			if (task == null) {
				task = DataManager.getInstance().getWorkNFTask(tnb);
			}
			if (task == null) {
				return false;
			}
			TaskServiceLabel serviceLabel = task.getServiceLabel(mki);
			if (serviceLabel != null) {
				// 保存服务标注内容
				serviceLabel.setCTNT(reportServiceContent);
			}
		} else {
			EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_TASK,
					context.getString(R.string.changeProcessFailed), false);
		}
		return false;
	}

	@Override
	public boolean receiveTRPT(BaseResponse response) {
		// TODO Auto-generated method stub
		SubmittaskprocssResponse resp = (SubmittaskprocssResponse) response;
		String sts = resp.getSubmitTaskProcessResponseVO().getStat();
		String tnb = resp.getSubmitTaskProcessResponseVO().getTnb();
		// 如果上报节点成功
		if ("S".equals(sts)) {
			String pid = resp.getSubmitTaskProcessResponseVO().getPcid();
			//如果是航班任务的TGET应答，通用任务的T_EGET应答
			if (pid.equals(MessageModel.TGET)||pid.equals(MessageModel.T_EGET)) { // 对自动发送的TGET特殊处理
				return true;
			}
			String atim = resp.getSubmitTaskProcessResponseVO().getAtim();
			TaskReceiveAction.getInstance().updateTaskList(tnb, pid, atim);
		}
		return true;
	}

	@Override
	public boolean receiveTTUD(BaseResponse response) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean receiveTRST(BaseResponse response) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean receiveTRBD(BaseResponse response) {
		// TODO Auto-generated method stub
		SubmittaskvnbResponse vbnResponse = (SubmittaskvnbResponse) response;
		SubmitTaskVnbResponseVO vbnResponseVO = vbnResponse.getSubmitTaskVnbResponseVO();
		String sts = vbnResponseVO.getSts();
		if ("S".equals(sts)) {
			String tnb = vbnResponseVO.getTnb();
			String vnb = vbnResponseVO.getVnb();
			Task task = DataManager.getInstance().getWorkTask(tnb);
			if (task != null) {
				// 修改上班任务的VNB
				task.getTaskInfo().setVNB(vnb);
				if (S.isNotEmpty(vnb)) {
					EffectManager.getInstance().ttsPlay(
							EffectManager.TTS_TYPE_TASK, context.getString(R.string.bindCarSucceed), false);
				} else {
					EffectManager.getInstance().ttsPlay(
							EffectManager.TTS_TYPE_TASK, context.getString(R.string.cancelBindCarSucceed), false);
				}
			}
		} else {
			String ems = vbnResponseVO.getEms();
			EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_TASK,
					ems, false);
		}
		return false;
	}

	@Override
	public boolean receiveTTRQ(BaseResponse response) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean receiveFLG4(BaseResponse response) {
		GetunfinishedtasklistsResponse resp = (GetunfinishedtasklistsResponse) response;
		if (resp.getGetunfinishedtasklistsresponsevo() != null && resp.getGetunfinishedtasklistsresponsevo().getHashmap() != null) {
			DataManager.getInstance().saveUnfinishedTnbMap(resp.getGetunfinishedtasklistsresponsevo().getHashmap());

			ThreadManager.getInstance().resleepForIQRYThread(10);	// 重新开始IQRYThread
			return true;
		}
		//如果没有body内容，或没有map结构，保存一个空的map
		DataManager.getInstance().saveUnfinishedTnbMap(new LinkedHashMap());
		ThreadManager.getInstance().resleepForIQRYThread(10);		// 重新开始IQRYThread
		return false;
	}

	@Override
	public boolean receiveFLG41(BaseResponse response) {
		GetUnfinishedEventTaskListsResponse resp = (GetUnfinishedEventTaskListsResponse) response;
		if (resp.getGetunfinishedeventtasklistsResponseVo() != null && resp.getGetunfinishedeventtasklistsResponseVo().getHashmap() != null) {
			DataManager.getInstance().saveUnfinishedEventTnbMap(resp.getGetunfinishedeventtasklistsResponseVo().getHashmap());

			ThreadManager.getInstance().resleepForIQRYThread(10);	// 重新开始IQRYThread
			return true;
		}
		//如果没有body内容，或没有map结构，保存一个空的map
		DataManager.getInstance().saveUnfinishedEventTnbMap(new LinkedHashMap());
		ThreadManager.getInstance().resleepForIQRYThread(10);		// 重新开始IQRYThread
		return false;
	}

	@Override
	public boolean receiveFLG1(BaseResponse response) {
		GetnewplantasklistsResponse resp = (GetnewplantasklistsResponse) response;
		if (resp.getGetnewplantasklistsresponsevo() != null && resp.getGetnewplantasklistsresponsevo().getHashmap() != null) {
			DataManager.getInstance().saveNewTnbMap(resp.getGetnewplantasklistsresponsevo().getHashmap());
			return true;
		}
		//如果没有body内容，或没有map结构，保存一个空的map
		DataManager.getInstance().saveNewTnbMap(new LinkedHashMap());
		return false;
	}
	
	@Override
	public boolean receiveTask(BaseResponse response){
		
		//接收任务处理
		GettaskbytnbResponse resp = (GettaskbytnbResponse) response;
		GetTaskByTnbResponseVO vo = resp.getGetTaskByTnbResponseVO();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String time = df.format(resp.getHeader()
				.getCurrentTimestamp());

		LinkedHashMap<String,Task> taskMap = vo.getHashmap();
		for(String tnb:taskMap.keySet()){

			DataManager.getInstance().removeResponseTnb(tnb);
			if(S.isNotEmpty(taskMap.get(tnb).getTNB()))
			{
				//接收任务处理
				TaskReceiveAction.getInstance().receiveTask(taskMap.get(tnb),MessageModel.ITSD,time);
			}

			try {
				Thread.sleep(30);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		return false;
	}

	@Override
	public boolean receiveQQRY(BaseResponse response) {
		GetGroupmemberResponse getGroupmemberResponse = (GetGroupmemberResponse) response;
		List<User> list = getGroupmemberResponse.getGetGroupmemberResponseVO().getUsers();
		if (list==null){
			list = new ArrayList<User>();
		}
		User[] userList = list.toArray(new User[list.size()]);
		//保存用户列表信息
		DataManager.getInstance().setMemberList(userList);
		ActionManager.getInstance().updateUI(MessageModel.QQRY);
		return false;
	}

	@Override
	public boolean receiveGHOU(BaseResponse response) {
		GetHandOverUserResponse getHandOverUserResponse = (GetHandOverUserResponse) response;
		List<User> list = getHandOverUserResponse.getGethandoveruserresponse().getUsers();
		if (list==null){
			list = new ArrayList<User>();
		}
		User[] userList = list.toArray(new User[list.size()]);
		//保存用户列表信息
		DataManager.getInstance().setMemberList(userList);
		ActionManager.getInstance().updateUI(MessageModel.GHOU);
		return false;
	}

	@Override
	public boolean receiveTTAS(BaseResponse response) {
		ForwardTaskResponse forwardTaskResponse = (ForwardTaskResponse) response;
		String tnb = forwardTaskResponse.getTnb();
		String ten = forwardTaskResponse.getTenb();
		Task sendtoTask = DataManager.getInstance().getWorkTask(tnb);
		if (sendtoTask == null) {
			return false;
		}
		TaskMonitorInfo info = forwardTaskResponse.getTaskMonitorInfo();
		if (null != info){
			if (JavaBeanManager.getInstance().adjust_pri(AuthEnum.AI_T_TASKSEARCH.toString())) {
				saveFLG10Task(info);
			}
			DataManager.getInstance().delWorkTask(tnb);
			DataManager.getInstance().delReceiveTask(tnb);
		}
		String msg = context.getString(R.string.RMT_5) + sendtoTask.getTaskInfo().getFLN() + context.getString(R.string.RMT_6) + sendtoTask.getTaskInfo().getRNM() + context.getString(R.string.RMT_9) + ten.replace("|", ",") + context.getString(R.string.RMT_8);
		BoxDBHelper dbHelper = new BoxDBHelper(DataBaseManager.getInstance().getContext());
		dbHelper.setTaskData(tnb, msg, MessageModel.TTAS, S.getNowTime3(), sendtoTask.getTaskInfo().getFKE(), sendtoTask.getTaskInfo().getFLN());
		dbHelper.closeDB();
		DataManager.getInstance().addUnCheckNumber();
		// 刷新任务列表UI或者刷新任务详情UI
		ActionManager.getInstance().refreshBox(MessageModel.REFRESH);
		EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_TASK, context.getString(R.string.forwardSucceed), false);
		return false;
	}

	@Override
	public boolean receiveTTAL(BaseResponse response) {
		TransferTaskResponse transferTaskResponse = (TransferTaskResponse) response;
		String tnb = transferTaskResponse.getTnb();
		String ten = transferTaskResponse.getTenb();//任务移交接收者
		EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_TASK, context.getString(R.string.transferSucceed), false);
		//删除任务记录
		String[] tnbs = tnb.split("\\|");
		BoxDBHelper dbHelper = new BoxDBHelper(DataBaseManager.getInstance().getContext());
		for (int i = 0; i < tnbs.length; i++) {
			Task mTask = DataManager.getInstance().getWorkTask(tnbs[i]);
			if (mTask == null) {
				continue;
			}
			String msg = context.getString(R.string.RMT_5) + mTask.getTaskInfo().getFLN() + context.getString(R.string.RMT_6) + mTask.getTaskInfo().getRNM() + context.getString(R.string.RMT_7) + ten + context.getString(R.string.RMT_8);
			dbHelper.setTaskData(tnbs[i], msg, MessageModel.TTAL, S.getNowTime3(), mTask.getTaskInfo().getFKE(), mTask.getTaskInfo().getFLN());
			DataManager.getInstance().addUnCheckNumber();
			DataManager.getInstance().delWorkTask(tnbs[i]);
		}
		dbHelper.closeDB();
		// 刷新任务列表UI或者刷新任务详情UI
		ActionManager.getInstance().refreshBox(MessageModel.REFRESH);
		return false;
	}

	@Override
	public boolean receiveUCBD(BaseResponse response) {
        ManCarBindingResponse manCarBindingResponse = (ManCarBindingResponse) response;
        String vnb = manCarBindingResponse.getVnb();
        UserInfoBean userinfo = JavaBeanManager.getInstance().getUserinfo();
        if (userinfo != null) {
            userinfo.setVnb(vnb);
            //将车号保存到接收任务中
            LinkedHashMap<String, Task> receiveList = DataManager.getInstance().getReceiveTaskList();
            if (receiveList != null) {
                Set<String> receiveSet = receiveList.keySet();
                for (String key : receiveSet) {

                    Task task = receiveList.get(key);
                    if (task != null && task.getTaskInfo() != null) {
                        task.getTaskInfo().setVNB(vnb);
                    }
                }
            }
            //将车号保存到工作任务中
            LinkedHashMap<String, Task> workList = DataManager.getInstance().getWorkTaskList();
            if (workList != null) {
                Set<String> workSet = workList.keySet();
                for (String key : workSet) {
                    Task task = workList.get(key);
                    if (task != null && task.getTaskInfo() != null) {
                        task.getTaskInfo().setVNB(vnb);
                    }
                }
            }
        }
		if(S.isEmpty(vnb)){
			EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_TASK, context.getString(R.string.cancelBindCarSucceed), false);
		}else {
			EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_TASK, context.getString(R.string.bindCarSucceed), false);
		}
        ActionManager.getInstance().updateUI(MessageModel.UCBD);
		return false;
    }

	@Override
	public boolean receiveFLG10(BaseResponse response) {
		FindForwardTaskResponse findForwardTaskResponse = (FindForwardTaskResponse) response;
		FindForwardTaskResponseVO findForwardTaskResponseVO = findForwardTaskResponse.getFindForwardTaskResponseVO();
		String ptm = findForwardTaskResponseVO.getPtm();
		String idx = findForwardTaskResponseVO.getIdx();
		if (null == idx){
			return false;
		}
		if ("N".equals(idx)) {
			JavaBeanManager.getInstance().saveFLG10IdxInfo(idx);
			if (S.isNotEmpty(ptm)) {
				JavaBeanManager.getInstance().saveFLG10SdxInfo(ptm);
			}
			return false;
		}
		List<TaskMonitorInfo> list = findForwardTaskResponseVO.getTaskMonitorInfos();
		for (TaskMonitorInfo task : list) {
			TaskInfo taskInfo = task.getTaskInfo();
			if ("update".equals(task.getFLAG())) {
				if (null == taskInfo) {
					continue;
				}
				String tnb = task.getTNB();
				String pid = task.getPID();
				String pidx = task.getPDX();
				boolean isFind = false;
				ArrayList<FLG10TaskBean> taskListInfo = JavaBeanManager.getInstance().getFLG10Taskinfo().getList();
				if (null == taskListInfo) {
					taskListInfo = new ArrayList<FLG10TaskBean>();
				}
				for (int i = 0; i < taskListInfo.size(); i++) {
					FLG10TaskBean flg10Task = taskListInfo.get(i);
					if (tnb.equals(flg10Task.getTnb())) {
						flg10Task.setFln(taskInfo.getFLN());
						flg10Task.setAod(taskInfo.getAOD());
						flg10Task.setReg(taskInfo.getREG());
						flg10Task.setPsn(taskInfo.getPSN());
						flg10Task.setAcn(taskInfo.getACN());
						flg10Task.setTime(TaskUtil.getFlightLatestTime(
								taskInfo.getATO(), taskInfo.getETO(),
								taskInfo.getSTO()));
						flg10Task.setTem(taskInfo.getTEM());
						flg10Task.setTst(task.getPCNM());
						flg10Task.setTnb(tnb);
						flg10Task.setTenb(taskInfo.getTEN());
						flg10Task.setPid(pid);
						flg10Task.setRnm(taskInfo.getRNM());
						flg10Task.setPidx(pidx);
						if (task.getPCNM().equals(MessageModel.TCXX)) {// 如果任务状态为取消，则提示班长任务取消
							ShowToast.get().show(taskInfo.getTEM() + context.getString(R.string.RMT_1)
									+ taskInfo.getFLN() + context.getString(R.string.RMT_4));// 提示任务状态变更
							EffectManager.getInstance().playSound(ParameterModel.MUSIC_EFFECT_RECEIVE_INFO);
							EffectManager.getInstance().vibrate();
						}
						isFind = true;
						break;
					}
				}
				if (!isFind) {
					saveFLG10Task(task);
				}
			} else if ("delete".equals(task.getFLAG())) {
				String tnb = task.getTNB();
				String tenb = task.getTENB();
				FLG10TaskInfoBean taskinfo = JavaBeanManager.getInstance().getFLG10Taskinfo();
				ArrayList<FLG10TaskBean> flg10TaskList = taskinfo.getList();
				if (null != flg10TaskList) {
					for (int i = 0; i < flg10TaskList.size(); i++) {
						FLG10TaskBean flg10task = flg10TaskList.get(i);
						if (flg10task.getTnb().equals(tnb) && flg10task.getTenb().equals(tenb)
								&& flg10task.getRnm().equals(task.getTaskInfo().getRNM())) {
							flg10TaskList.remove(flg10task);
							i--;
						}
					}
				}
			}
		}
		JavaBeanManager.getInstance().saveFLG10SdxInfo(ptm);
		int intIdx = Integer.parseInt(idx);
		if (intIdx > 0){
			intIdx = intIdx - 1;
		}
		JavaBeanManager.getInstance().saveFLG10IdxInfo(S.valueOf(intIdx));
		ThreadManager.getInstance().resleepForIQRYThread(10);
		ActionManager.getInstance().updateUI(MessageModel.ITTL);
		return false;
	}

	@Override
	public boolean receiveGTFL(BaseResponse response) {
		TaskTransferredResponse taskTransferredResponse = (TaskTransferredResponse) response;
		TaskTransferredResponseVO taskTransferredResponseVO = taskTransferredResponse.getTaskTransferredResponseVO();
		if (taskTransferredResponseVO != null){
			List<TaskForward> list = taskTransferredResponseVO.getTfvos();
			JavaBeanManager.getInstance().setTaskForwardList(list);
			JavaBeanManager.getInstance().saveGTFLInfo("S");

			ThreadManager.getInstance().resleepForIQRYThread(10);
		}
		return false;
	}

	/**
	 * 保存FLG10任务
	 *
	 */
	private void saveFLG10Task(TaskMonitorInfo task) {
		if (null != task) {
			String tnb = task.getTNB();
			String pidx = task.getPDX();
			TaskInfo taskInfo = task.getTaskInfo();
			if (null != taskInfo) {
				FLG10TaskBean flg10Task = new FLG10TaskBean();
				flg10Task.setFln(taskInfo.getFLN());
				flg10Task.setAod(taskInfo.getAOD());
				flg10Task.setReg(taskInfo.getREG());
				flg10Task.setPsn(taskInfo.getPSN());
				flg10Task.setAcn(taskInfo.getACN());
				flg10Task.setTime(TaskUtil.getFlightLatestTime(taskInfo.getATO(),
						taskInfo.getETO(), taskInfo.getSTO()));
				flg10Task.setTem(taskInfo.getTEM());
				flg10Task.setTst(task.getPCNM());
				flg10Task.setTnb(tnb);
				flg10Task.setPid(task.getPID());
				flg10Task.setRnm(taskInfo.getRNM());
				flg10Task.setPidx(pidx);
				JavaBeanManager.getInstance().saveFLG10TaskData(flg10Task);
			}
		}
	}

    @Override
    public boolean receiveULBC(BaseResponse response) {
        List<LuggageBarCode> changeStateList = new ArrayList<>();
        UploadLuggageResponse uploadLuggageResponse = (UploadLuggageResponse) response;
        List<UploadLuggageResultVO> list = uploadLuggageResponse.getUploadResultList();
        for (UploadLuggageResultVO vo : list) {
            LuggageBarCode luggageBarCode = new LuggageBarCode();
            luggageBarCode.setBarcode(vo.getBarcode());
            luggageBarCode.setTnb(vo.getTnb());
            luggageBarCode.setEnb(JavaBeanManager.getInstance().getUserinfo().getEnb());
            if (vo.isFlag()) {
                luggageBarCode.setState(LuggageBarCode.STATE_SENT);
            } else {
                luggageBarCode.setState(LuggageBarCode.STATE_SEND_FAILED);
            }
            changeStateList.add(luggageBarCode);
        }
        BoxDBHelper boxDBHelper = new BoxDBHelper(DataBaseManager.getInstance().getContext());
        boxDBHelper.setMultiLuggageBarCodeState(changeStateList, null);
        boxDBHelper.closeDB();
        ThreadManager.getInstance().reSleepSendLuggageBarCodeThread(10000);
        return false;
    }

    @Override
    public boolean receiveDLBC(BaseResponse response) {
        DeleteLuggageResponse deleteLuggageResponse = (DeleteLuggageResponse) response;
        LuggageBarCode luggageBarCode = new LuggageBarCode();
        luggageBarCode.setEnb(JavaBeanManager.getInstance().getUserinfo().getEnb());
        luggageBarCode.setTnb(deleteLuggageResponse.getTnb());
        luggageBarCode.setBarcode(deleteLuggageResponse.getBarcode());
        BoxDBHelper boxDBHelper = new BoxDBHelper(DataBaseManager.getInstance().getContext());
        if (deleteLuggageResponse.isDeleteFlag()) {
            boxDBHelper.deleteOneLuggageBarCode(luggageBarCode);
        } else {
            luggageBarCode.setState(LuggageBarCode.STATE_DELETE_FAILED);
            boxDBHelper.setOneLuggageBarCodeState(luggageBarCode);
            EffectManager.getInstance().ttsPlay(EffectManager.TTS_TYPE_TASK, context.getString(R.string.deleteBarCodeFailed), false);
        }
        boxDBHelper.closeDB();
        return false;
    }

    @Override
    public boolean receiveQLBC(final BaseResponse response) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                QueryLuggageResponse queryLuggageResponse = (QueryLuggageResponse) response;
                Map<String, List<LuggageBarCode>> map = queryLuggageResponse.getQueryResultMap();
                if (map != null && map.size() > 0) {
                    BoxDBHelper boxDBHelper = new BoxDBHelper(DataBaseManager.getInstance().getContext());
                    for (String tnb : map.keySet()) {
                        Task task = DataManager.getInstance().getWorkTask(tnb);
                        if (null != task) {
                            List<LuggageBarCode> list = map.get(tnb);
                            sortList(list);
                            boxDBHelper.saveQueryLuggageResponse(list, tnb);
                        }
                    }
                    boxDBHelper.closeDB();
                }
                JavaBeanManager.getInstance().setInitQueryLuggage(true);
            }
        }).start();
        return false;
    }

    private void sortList(List<LuggageBarCode> list) {
        Collections.sort(list, new Comparator<LuggageBarCode>() {
            public int compare(LuggageBarCode arg0, LuggageBarCode arg1) {
                return arg0.getScanTime().compareTo(arg1.getScanTime());
            }
        });
    }

	@Override
	public boolean receiveWGLM(BaseResponse response) {
		WorkGroupLoadMembersResponse workGroupLoadMembersResponse = (WorkGroupLoadMembersResponse) response;
		WorkGroupLoadMembersResponseVO responseVO = workGroupLoadMembersResponse.getWorkGroupLoadMemberResponseVO();
		if (null != responseVO) {
			JavaBeanManager.getInstance().setWorkGroupLoadInfo(responseVO);
		}
		return false;
	}

	@Override
	public boolean receiveWGAM(BaseResponse response) {
		WorkGroupAddMemberResponse addMemberResponse = (WorkGroupAddMemberResponse) response;
		boolean addSuccess = addMemberResponse.getHeader().getStatus() == BaseResponseHeader.STATUS_SUCCESS;

		if (addSuccess) {
			WorkGroupAddMemberResponseVO responseVO = addMemberResponse.getWorkGroupAddMemberResponseVO();
			WorkGroupLoadMembersResponseVO info = JavaBeanManager.getInstance().getWorkGroupLoadInfo();
			if (responseVO != null && responseVO.getMember() != null) {
				if (info == null) {
					info = new WorkGroupLoadMembersResponseVO();
					JavaBeanManager.getInstance().setWorkGroupLoadInfo(info);
				}
				info.getMembers().add(responseVO.getMember());
			}
		}

		return false;
	}

	@Override
	public boolean receiveWGDM(BaseResponse response) {
		WorkGroupDeleteMembersResponse deleteMembersResponse = (WorkGroupDeleteMembersResponse) response;
		boolean removeSuccess = deleteMembersResponse.getHeader().getStatus() == BaseResponseHeader.STATUS_SUCCESS;

		// 移除成功，移除数据缓存。
		if (removeSuccess) {
			WorkGroupLoadMembersResponseVO workGroupLoadInfo = JavaBeanManager.getInstance().getWorkGroupLoadInfo();
		 	List<Member> listMembers = deleteMembersResponse.getMembers();		// 获取删除人员列表
			if (workGroupLoadInfo != null && workGroupLoadInfo.getMembers() != null && listMembers != null) {
				List<Member> infoMembers = workGroupLoadInfo.getMembers();

				for (Member member: listMembers) {
					String member_usno = member.getMember_usno();

					Iterator<Member> iterator = infoMembers.iterator();
					while (iterator.hasNext()) {
						Member next = iterator.next();
						if (member_usno.equals(next.getMember_usno())) {
							iterator.remove();
							break;
						}
					}
				}
			}
		}

		return false;
	}

	@Override
	public boolean receiveFXLI(BaseResponse response) {
		FindLuggageResponse findLuggageResponse = (FindLuggageResponse) response;
		List<FindLuggageInfoResponseVO> listVO = findLuggageResponse.getFindLuggageInfoResponseVO();
		if (listVO == null) {
			return false;
		}
		for(FindLuggageInfoResponseVO luggageInfoResponseVO:listVO) {
			FindLuggageDataManager.getInstance().addListFindLuggageInfo(luggageInfoResponseVO);
			String tnb = luggageInfoResponseVO.getTaskId();
			//删除需要初始化翻行李信息的任务号
			FindLuggageDataManager.getInstance().delFindLuggageTnb(tnb);
		}
		return false;
	}

	@Override
	public boolean receiveFXLR(BaseResponse response) {
        boolean removeSuccess = response.getHeader().getStatus() == BaseResponseHeader.STATUS_SUCCESS;
        if (!removeSuccess) {
            return false;
        }

        ReportFindLuggageResponse reportFindLuggageResponse = (ReportFindLuggageResponse) response;
        ReportFindLuggageResponseVO reportFindLuggageResponseVO = reportFindLuggageResponse.getReportFindLuggageResponseVO();
        if (reportFindLuggageResponseVO == null) {
            return false;
        }

        String taskId = reportFindLuggageResponseVO.getTaskId();
        String messageId = reportFindLuggageResponseVO.getMessageId();
        List<LuggageVO> listReportLuggage = reportFindLuggageResponseVO.getLuggages();
        // 任务号为空
        if (TextUtils.isEmpty(taskId)) {
            return false;
        }

        // 不包含行李号的上报
        if (listReportLuggage == null || listReportLuggage.isEmpty()) {
            return true;    // 不包含行李号，数据状态不需要更新，页面不需要更新。
        }

        // 本地对应任务下翻行李消息
        LuggageMessageVO luggageMessage = FindLuggageDataManager.getInstance().getLuggageMessageByMsgId(taskId, messageId);
        if (luggageMessage == null || !luggageMessage.getMessageId().equals(messageId)) {
            return false;
        }

        // 遍历上报的行李号，修改其状态
        for (LuggageVO reportLuggage : listReportLuggage) {
            if (reportLuggage == null || reportLuggage.getLuggageId() == null) break;
            for (LuggageVO luggage : luggageMessage.getLuggages()) {
                if (luggage == null || luggage.getLuggageId() == null) break;
                if (reportLuggage.getLuggageId().equals(luggage.getLuggageId())) {
                    luggage.setLuggageState(reportLuggage.getLuggageState());
                    break;
                }
            }
        }
        return true;
    }
}
